package com.pet.shop.controller;

import com.pet.shop.model.Goods;
import com.pet.shop.model.User;
import com.pet.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author Hxx
 * @Description //TODO 登录注册
 * @Date 18:17 2020/3/6
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/user")
public class LoginAndRegister {

    @Autowired
    private UserService userService;

    /**
     * @return com.pet.shop.model.RespBean
     * @Author Hxx
     * @Description //TODO 用户登录
     * @Date 18:17 2020/3/6
     * @Param [user, httpSession]
     **/
    @PostMapping("/login")
    @ResponseBody
    public Integer login(User user, HttpServletRequest request) {
        User user1=userService.findByphone(user.getPhone());
        request.getSession().setAttribute("User",user1);

        return userService.login(user) ;

    }

    /**
     * 用户注册
     *
     * @param user
     * @return Integer 0:注册成功 1:用户名重复
     */

    @PostMapping("/register")
    @ResponseBody
    public Integer register(User user) {
        return userService.register(user);
    }

    /**
     * 管理员登录
     *
     * @param user
     * @param httpSession
     * @return
     */
    @ResponseBody
    @PostMapping("/adminLogin")
    public Integer adminLogin(User user, HttpSession httpSession) {
        Integer flag = userService.adminLogin(user);
        if (flag == 0) {
            httpSession.setAttribute("admin", userService.adminLogin(user));
            httpSession.setMaxInactiveInterval(60 * 60 * 24);
        }
        return flag;
    }

    /**
     * 查看所有用户
     */
    @GetMapping("/queryAllUser")
    public String queryAllUser(Model model) {
        model.addAttribute("Users", userService.findAllUser());
        return "admin/admin-list";
    }
}
